package aar.test.tessocr;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import aar.test.tessocr.base.BaseActivity;
import aar.test.tessocr.base.BaseFragmentPagerAdapter;
import aar.test.tessocr.baseApplication.BaseApplication;
import aar.test.tessocr.fragment.OrderTakenFragment;
import aar.test.tessocr.fragment.OrderUnTakenFragment;
import aar.test.tessocr.utils.PrefUtils;
import aar.test.tessocr.view.SearchHistoryPopwindow;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import clx.tessocr.ocreng.R;
import lib_zxing.activity.CodeUtils;

/**
 * 揽件管理
 * Created by sunshine on 2020/10/20
 */
public class LanListActivity extends BaseActivity implements SearchHistoryPopwindow.OnSearchListener {
    @BindView(R.id.tbl_recruit)
    TabLayout tblOrder;
    @BindView(R.id.vp_content)
    ViewPager viewPager;
    @BindView(R.id.et_fragment_search)
    TextView etFragmentSearch;
    @BindView(R.id.view_line)
    View viewLine;
    private BaseFragmentPagerAdapter mAdapter;
    private Integer[] titlesRes = new Integer[]{R.string.order_untaken, R.string.order_already_taken};
    private String search = "";
    private String type = "";
    private String cabinetId = "";
    private Button btn_cabinet_login;
    private TextView txt_cabinet_code;
    private RelativeLayout ll_cabinet_num;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        type = getIntent().getStringExtra("type");
        cabinetId = PrefUtils.getString(this, "cabinetId", "");
        setContentView(R.layout.activity_common_tab);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        btn_cabinet_login = findViewById(R.id.login_button_cabinet);
        txt_cabinet_code = findViewById(R.id.txt_cabinet_code);
        ll_cabinet_num = findViewById(R.id.ll_cabinet_num);
        ll_cabinet_num.setVisibility(View.VISIBLE);

        btn_cabinet_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent1 = new Intent(LanListActivity.this, ScanLoginCabActivity.class);
                // 合合信息授权提供的APP_KEY
                intent1.putExtra("from", "isFromLogin");
                startActivityForResult(intent1, 100);
            }
        });

        List<String> titles = new ArrayList<>();
        for (int i = 0; i < titlesRes.length; i++) {
            titles.add(getResources().getString(titlesRes[i]));
        }

        List<Fragment> fragments = new ArrayList<>();

        fragments.add(OrderUnTakenFragment.newInstance("1", search, cabinetId));
        fragments.add(OrderTakenFragment.newInstance("2", search, cabinetId, false));

        mAdapter = new BaseFragmentPagerAdapter(getSupportFragmentManager(), fragments, titles);
        viewPager.setAdapter(mAdapter);
        viewPager.setOffscreenPageLimit(fragments.size() - 1);
        tblOrder.setupWithViewPager(viewPager);
        etFragmentSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new SearchHistoryPopwindow(LanListActivity.this, search, true, LanListActivity.this, "orderSearchContent").showAtLocation(viewLine, Gravity.TOP, 0, 0);
            }
        });
    }

    @Override
    public void onSearch(String searchContent) {
        search = searchContent;
        etFragmentSearch.setText(search);
        mAdapter = null;
        List<String> titles = new ArrayList<>();
        for (int i = 0; i < titlesRes.length; i++) {
            titles.add(getResources().getString(titlesRes[i]));
        }

        List<Fragment> fragments = new ArrayList<>();


        fragments.add(OrderUnTakenFragment.newInstance("1", search, cabinetId));
        fragments.add(OrderTakenFragment.newInstance("2", search, cabinetId, false));

        mAdapter = new BaseFragmentPagerAdapter(getSupportFragmentManager(), fragments, titles);
        //viewPager = null;
        //viewPager = new ViewPager(RecruitmentActivity.this);
        viewPager.setAdapter(mAdapter);
        viewPager.setOffscreenPageLimit(fragments.size() - 1);
        tblOrder.setupWithViewPager(viewPager);
    }

    @Override
    public void onBack() {
        finish();
    }

    @OnClick(R.id.rl_back)
    public void onViewClicked() {
        finish();
    }

    @Override
    protected void onResume() {
        super.onResume();

        if (PrefUtils.getBoolean(this, "cabinetLoginFlag", false)) {
            btn_cabinet_login.setText(getString(R.string.login_cab_success_tip));
            txt_cabinet_code.setText(PrefUtils.getString(this, "cabinetCode", "无"));
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (null != data) {
                int mm = data.getIntExtra(CodeUtils.RESULT_TYPE, CodeUtils.RESULT_FAILED);
                if (mm == CodeUtils.RESULT_SUCCESS) {
                    btn_cabinet_login.setText(getString(R.string.login_cab_success_tip));
                    txt_cabinet_code.setText(BaseApplication.getIntance().getLoginCabModel().getCabinetcode());
                    cabinetId = BaseApplication.getIntance().getLoginCabModel().getCabinetid();

                    mAdapter = null;
                    List<String> titles = new ArrayList<>();
                    for (int i = 0; i < titlesRes.length; i++) {
                        titles.add(getResources().getString(titlesRes[i]));
                    }

                    List<Fragment> fragments = new ArrayList<>();


                    fragments.add(OrderUnTakenFragment.newInstance("1", search, cabinetId));
                    fragments.add(OrderTakenFragment.newInstance("2", search, cabinetId, true));

                    mAdapter = new BaseFragmentPagerAdapter(getSupportFragmentManager(), fragments, titles);
                    //viewPager = null;
                    //viewPager = new ViewPager(RecruitmentActivity.this);
                    viewPager.setAdapter(mAdapter);
                    viewPager.setOffscreenPageLimit(fragments.size() - 1);
                    tblOrder.setupWithViewPager(viewPager);
                } else if (mm == CodeUtils.RESULT_FAILED) {
                    btn_cabinet_login.setText(getString(R.string.login_cab));
                }
            }
        }
    }
}

